import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class demo54 {
    public static void main(String[] args) throws InterruptedException {
        //把一个任务分成了10个子任务
        CountDownLatch countDownLatch=new CountDownLatch(10);
        //创建了4个核心线程
        ExecutorService executorService= Executors.newFixedThreadPool(4);
        for(int i=0;i<10;i++){
            int id=i;
            executorService.submit(()->{
                System.out.println("开始执行子任务"+id);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("结束执行子任务"+id);
                //确保当前子任务完成
                countDownLatch.countDown();
            });
        }
        //这个方法阻塞等待，确保所有子任务都完成
        countDownLatch.await();
        System.out.println("所有任务执行完毕");
        executorService.shutdown();
    }
}
